/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gedigarr.grid;

import gedigarr.objects.CharacterCell;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import putil.Putil;

/**
 *
 * @author JC
 */
public abstract class TextGrid implements GridInterface<CharacterCell> {

  public static final int FLAG_HORIZONTAL = 1 << 0;
  //public static final int FLAG_VERTICAL = 1 << 1;
  public static final int FLAG_CENTER = 1 << 2;
  protected CharacterCell[][] grid;
  private Font font = new Font( "Courier New", Font.BOLD, 20 );
  /*
  public void drawString( String text, Color fgColour, int x, int y, int width, int height, int flags ) {
  int length = text.length();
  if ( Putil.isBitSet( flags, FLAG_CENTER ) ) {
  x -= Math.round( length / 2.0 );
  }
  
  for ( int i = 0; i < length; i++ ) {
  char c = text.charAt( i );
  
  if ( x < 0 || x >= width
  || y < 0 || y >= height ) {
  if ( Putil.isBitSet( flags, FLAG_HORIZONTAL ) ) {
  x++;
  }
  if ( Putil.isBitSet( flags, FLAG_VERTICAL ) ) {
  y++;
  }
  continue;
  }
  
  grid[y][x].setFGColour( fgColour );
  grid[y][x].setChar( c );
  
  
  if ( Putil.isBitSet( flags, FLAG_HORIZONTAL ) ) {
  x++;
  }
  if ( Putil.isBitSet( flags, FLAG_VERTICAL ) ) {
  y++;
  }
  }
  }
   *
   */
  /*
  public void drawString( String text, Color fgColour, int x, int y, int width, int height, int flags ) {
  int length = text.length();
  int charX = x, charY = y;
  if ( Putil.isBitSet( flags, FLAG_CENTER ) ) {
  x -= Math.round( length / 2.0 );
  charX = x;
  }
  System.out.println( text );
  
  for ( int i = 0; i < length; i++ ) {
  char c = text.charAt( i );
  
  if ( charX < x || charX >= x + width
  || charY < y || charY >= y + height ) {
  if ( Putil.isBitSet( flags, FLAG_HORIZONTAL ) ) {
  charX++;
  }
  if ( Putil.isBitSet( flags, FLAG_VERTICAL ) ) {
  charY++;
  }
  continue;
  }
  grid[charY][charX].setFont( this.font );
  grid[charY][charX].setFGColour( fgColour );
  grid[charY][charX].setChar( c );
  
  
  if ( Putil.isBitSet( flags, FLAG_HORIZONTAL ) ) {
  charX++;
  }
  if ( Putil.isBitSet( flags, FLAG_VERTICAL ) ) {
  charY++;
  }
  }
  }*/

  public void drawString( String text, Color fgColour, int x, int y, int width, int height, int flags ) {
    int length = text.length();
    int charX, charY;
    int xO = x;
    Rectangle gridBounds = new Rectangle( 0, 0, this.getColumns(), this.getRows() );

    for ( int rY = 0, index = 0; rY < height; rY++ ) {
      if ( Putil.isBitSet( flags, FLAG_CENTER ) ) {
        //x = x - ( length / 2 );
        xO = x - (Math.min( length, width ) / 2);
      }
      for ( int rX = 0; length > 0 && rX < width; rX++ ) {
        charX = xO + rX;
        charY = y + rY;
        if ( gridBounds.contains( charX, charY ) ) {
          char c = text.charAt( index );
          grid[charY][charX].setFont( this.font );
          grid[charY][charX].setFGColour( fgColour );
          grid[charY][charX].setChar( c );
        }
        length--;
        index++;
      }

    }
  }

  public void clearGrid() {
    clearGrid( 0, 0, getColumns(), getRows() );
  }

  public void clearGrid( int x, int y, int width, int height ) {
    for ( int yD = y; yD < height; yD++ ) {
      for ( int xD = x; xD < width; xD++ ) {
        grid[yD][xD].setSolid( false );
        grid[yD][xD].setVisible( false );
        grid[yD][xD].setBGColour( Color.BLACK );
        grid[yD][xD].setFGColour( Color.BLACK );
        grid[yD][xD].setChar( ' ' );
      }
    }
  }

  protected void setFont( Font font ) {
    this.font = font;
  }

  public int getRows() {
    return grid == null ? 0 : grid.length;
  }

  public int getColumns() {
    return getRows() == 0 ? 0 : grid[0] == null ? 0 : grid[0].length;
  }

  /*
  public void setCursor( int x, int y ) {
  setActive( pos.x, pos.y, false );
  pos.setLocation( x, y );
  setActive( x, y, true );
  }
   */
  public CharacterCell get( int x, int y ) {
    return grid[y][x];
  }
}
